<?php
/**
 * Created by PhpStorm.
 * 商家店铺管理
 * User: caicm
 * Date: 2017/6/14
 * Time: 17:43
 */

namespace WMApi\Controller;



use WMApi\Model\WmSellerModel;
use WMApi\Model\WmStoreClassifyModel;
use WMApi\Service\SellerStoreService;
use SDXApi\Service\OssUploadService;
use Common\Api\Ali_ossApi;

class SellerStoreController extends SellerBaseController  {

    /**
     * 店铺申请
     * Created By caicm
     * @param int seller_id 商家id
     * @param string store_name 店铺名称
     * @param  string store_introduce 店铺描述
     * @param int cat_id 分类id(必须选择二级分类)
     * @param string province 省份
     * @param string city 城市
     * @param string district 区
     * @param string address 地址
     * @param string sign_building 地标
     * @param string address_name 门牌号
     * @param string contacts 联系人
     * @param int contacts_phone 联系人电话
     * @param string start_time 开始（营业时间）
     * @param string end_time 结束（营业时间）
     * @param int starting_money 起送金额
     * @param int shipping_fee 配送费用
     * @param int store_sw 店铺开关（1：营业，2：休息）
     * @param string store_img 图片地址
     * @param int shipping_way_id 配送方式 1-周边配 2-干线全城配
     * @param int shipping_radius 配送半径（公里）
     */
    public function apply_store(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_name = I('post.store_name');
        $store_introduce = I('post.store_introduce');
        $classify_id = I('post.classify_id', 0, 'int');
        $province = I('post.province');
        $city = I('post.city');
        $district = I('post.district');
        $address = I('post.address');
        $sign_building = I('post.sign_building');
        $address_name = I('post.address_name');
        $contacts = I('post.contacts');
        $contacts_phone = I('post.contacts_phone');
        $start_time = I('post.start_time');
        $end_time = I('post.end_time');
        $starting_money = I('post.starting_money');
        $shipping_fee = I('post.shipping_fee');
        $store_sw = I('post.store_sw', 1, 'int');
        $store_img = I('post.store_img');
        $shipping_way = I('post.shipping_way_id', 1, 'int');
        $shipping_radius = I('post.shipping_radius');
        //|| empty($starting_money)
        if(empty($store_img) || empty($start_time) || empty($end_time) || empty($seller_id) || empty($store_name) || empty($store_introduce) || empty($classify_id) || empty($contacts) || empty($contacts_phone)){
            returnApiError('参数有误');
        }

        if(empty($province) || empty($city) || empty($district) || empty($address) || empty($sign_building) || empty($address_name)){
            returnApiError('地址参数有误');
        }
        if( abslength($store_name)>15){
            returnApiError("店铺名称不能超过15位");
        }
        if(!is_money($starting_money)){
            returnApiError("起送金额格式有误");
        }
        if(!in_array($store_sw,array(1,2))){
            returnApiError("store_sw 有误");
        }
        if(!is_mobile($contacts_phone)){
            returnApiError("请输入正确的手机号");
        }
        if($shipping_fee && !is_money($shipping_fee)){
            returnApiError("配送费用格式有误");
        }

        $store_service = new SellerStoreService();
        $seller_model = new WmSellerModel();

        $seller_info = $seller_model->find_seller_info($seller_id);
        if(!$seller_info){
            returnApiError("商家不存在");
        }

        $store_id = $store_service->apply_store($seller_id, $store_name, $store_introduce, $classify_id, $province, $city, $district, $address, $sign_building, $address_name, $contacts, $contacts_phone, $start_time, $end_time, $starting_money, $store_sw, $store_img, $shipping_fee, $shipping_way, $shipping_radius);

        if($store_id){
            returnApiSuccess(array('store_id'=>$store_id), '申请成功！');
        }else{
            returnApiError('申请失败！');
        }

    }

    /**
     * 店铺编辑
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     * @param string store_name 店铺名称
     * @param  string store_introduce 店铺描述
     * @param int cat_id 分类id(必须选择二级分类)
     * @param string province 省份
     * @param string city 城市
     * @param string district 区
     * @param string address 地址
     * @param string sign_building 地标
     * @param string address_name 门牌号
     * @param string contacts 联系人
     * @param int contacts_phone 联系人电话
     * @param string start_time 开始（营业时间）
     * @param string end_time 结束（营业时间）
     * @param int starting_money 起送金额
     * @param int shipping_fee 配送费用
     * @param int store_sw 店铺开关（1：营业，2：休息）
     * @param string store_img 图片地址
     * @param int shipping_way_id 配送方式 1-周边配 2-干线全城配
     * @param int shipping_radius 配送半径（公里）
     * @param int is_default 是否默认店铺 0-否 1-是
     */
    public function edit_store(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        $store_name = I('post.store_name');
        $store_introduce = I('post.store_introduce');
        $classify_id = I('post.classify_id', 0, 'int');
        $province = I('post.province');
        $city = I('post.city');
        $district = I('post.district');
        $address = I('post.address');
        $sign_building = I('post.sign_building');
        $address_name = I('post.address_name');
        $contacts = I('post.contacts');
        $contacts_phone = I('post.contacts_phone');
        $start_time = I('post.start_time');
        $end_time = I('post.end_time');
        $starting_money = I('post.starting_money');
        $shipping_fee = I('post.shipping_fee');
        $store_sw = I('post.store_sw', 1, 'int');
        $store_img = I('post.store_img');
        $shipping_way = I('post.shipping_way_id', 1, 'int');
        $shipping_radius = I('post.shipping_radius');
        $is_default = I('post.is_default', 0, 'int');


        //empty($starting_money) 暂时注销

        if(  empty($start_time) || empty($end_time) || empty($seller_id) || empty($store_id) || empty($store_name) || empty($store_introduce) || empty($contacts) || empty($contacts_phone)){
            returnApiError('参数有误');
        }

        if( abslength($store_name)>15){
            returnApiError("店铺名称不能超过15位");
        }
        if(!is_money($starting_money)){
            returnApiError("起送金额格式不正确");
        }
        if(!in_array($store_sw,array(1,2))){
            returnApiError("store_sw 有误");
        }
        if(!is_mobile($contacts_phone)){
            returnApiError("请输入正确的手机号");
        }
        if($shipping_fee && !is_money($shipping_fee)){
            returnApiError("配送费用格式有误");
        }

        $store_service = new SellerStoreService();
        $store_id = $store_service->edit_store($seller_id, $store_id, $store_name, $store_introduce, $classify_id, $province, $city, $district, $address, $sign_building, $address_name, $contacts, $contacts_phone, $start_time, $end_time, $starting_money, $store_sw, $store_img, $shipping_fee, $shipping_way, $shipping_radius, $is_default);

        if($store_id){
            returnApiSuccess(null, '操作成功！');
        }else{
            returnApiError('操作失败！');
        }

    }

    /**
     * 获取店铺列表
     * Created By caicm
     * @param int seller_id 商家id
     */
    public function get_store_list(){
        $seller_id = I('post.seller_id', 0, 'int');
        if(empty($seller_id) ){
            returnApiError('参数有误');
        }

        $store_model = new SellerStoreService();

        $store_list = $store_model->get_store_list($seller_id);

        if($store_list){
            returnApiSuccess(array('store_list'=>$store_list));
        }else{
            returnApiError('无数据');
        }


    }

    /**
     * 设置店铺为当前店铺
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     */
    public function edit_store_is_default(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        if(empty($seller_id) || empty($store_id)){
            returnApiError('参数有误');
        }

        $store_model = new SellerStoreService();
        $edit_store = $store_model->edit_store_is_default($seller_id, $store_id);

        if($edit_store){
            returnApiSuccess(null,'操作成功');
        }else{
            returnApiError('操作失败');
        }


    }

    /**
     * 获取店铺运营分类
     * Created By caicm
     */
    public function get_classlfy_list(){
        $classlfy_model = new WmStoreClassifyModel();
        $classlfy_list = $classlfy_model->find_classify_list(0);
        foreach ($classlfy_list as $key=>$value){
            $classlfy_list[$key]['subclass'] = $classlfy_model->find_classify_list($value['classify_id']);
        }

        if($classlfy_list){
            returnApiSuccess(array('classlfy_list'=>$classlfy_list));
        }else{
            returnApiError('无数据！');
        }

    }

    /**
     * 上传图片-base64
     * Created By caicm
     * @param string img base64
     */
    public function upload_img_base64(){
        $img_base64 = I('post.img');

        if (empty($img_base64)) {
            returnApiError('参数有误');
        }

        $img_base64 = substr(strstr($img_base64,','),1);
        $img = base64_decode($img_base64);
        $img_name = $this->seller_id."_".time().rand(10000,9999).".jpg";
        $path = APP_PATH."Runtime/Temp/".$img_name;//接收文件目录
        file_put_contents($path, $img);

        $osspath = 'data/waimai/store/'.date('Ym').'/'.$img_name;

        Ali_ossApi::upload($osspath,$path);

        returnApiSuccess(array('img_path'=>$osspath),'Upload Success');
    }

    /**
     * 上传图片-文件上传
     * Created By caicm
     * @param string img base64
     */
    public function upload_img_file(){

        if($_FILES['img']['name']){
            $img_name = $this->seller_id."_".time().rand(10000,9999) . ".".end(explode('.', $_FILES['img']['name']));
            $path = 'data/waimai/store/'.date('Ym').'/'.$img_name;
            Ali_ossApi::upload($path, $_FILES['img']['tmp_name']);
            returnApiSuccess(array('img_path'=>$path),'Upload Success');
        }else{
            returnApiError('没有上传文件');
        }
    }

    /**
     * 签名上传用例
     * Created By caicm
     */
    public function get_oss_upload_token()
    {
        $seller_id = I('post.seller_id', 0, 'int');
        if(empty($seller_id)){
            returnApiError('seller_id is null');
        }
        $ossService = new OssUploadService();
        $token = $ossService->getToken('data/waimai/store/'.date('Ym').'/', uniqid($seller_id.'_'));

        unset($token['fileName']);

        returnApiSuccess($token);
    }

    /**
     * 配送方式
     * Created By caicm
     */
    public function get_shipping_way(){

        foreach (get_shipping_way() as $val){
            $list[] = $val;
        }
        $data['shipping_way'] = $list;
        returnApiSuccess($data);
    }

    /**
     * 获取店铺营业时间
     * Created By caicm
     */
    public function get_open_store_time(){
        $data['open_store_time'] = get_open_store_time();
        returnApiSuccess($data);
    }

    /**
     * 获取配送半径
     * Created By caicm
     */
    public function get_shipping_radius(){
        $data['shipping_radius'] = get_shipping_radius();
        returnApiSuccess($data);
    }
}